package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.aitoedu.constant.MessageConstant;
import com.example.aitoedu.dto.MyCourseDTO;
import com.example.aitoedu.entity.Course;
import com.example.aitoedu.entity.Sc;
import com.example.aitoedu.mapper.CourseMapper;
import com.example.aitoedu.mapper.ScMapper;
import com.example.aitoedu.service.ScService;
import com.example.aitoedu.vo.StuCourseVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 曹正豪
* @description 针对表【sc(选课信息)】的数据库操作Service实现
* @createDate 2024-10-14 19:17:58
*/
@Service
@Slf4j
public class ScServiceImpl extends ServiceImpl<ScMapper, Sc>
    implements ScService{

    @Autowired
    private ScMapper scMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public SaResult joinClass(Integer code) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        Course course = courseMapper.selectOne(new QueryWrapper<Course>().eq("code", code));
        if(course == null) return SaResult.error(MessageConstant.SC_CODE_FOUND);
        Sc sc = scMapper.selectOne(new QueryWrapper<Sc>().eq("sid",sid).eq("cid",course.getId()));
        if (sc != null) return SaResult.error(MessageConstant.JOIN_ALREADY).setCode(code);
        Sc sc1 = Sc.builder()
                .cid(course.getId())
                .sid(sid)
                .joinTime(LocalDateTime.now())
                .build();
        scMapper.insert(sc1);
        return SaResult.ok(MessageConstant.JOIN_SUCCESS);
    }

    @Override
    public List<StuCourseVO> myCourse(MyCourseDTO myCourseDTO) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<StuCourseVO> stuCourseVOList = scMapper.myCourse(sid,myCourseDTO);
        log.debug("全部课程：{}",stuCourseVOList);
        return stuCourseVOList;
    }
}




